package com.alvin.springshiro.service.impl;

import com.alvin.springshiro.dao.PermissionMapper;
import com.alvin.springshiro.dao.RoleMapper;
import com.alvin.springshiro.dao.UserMapper;
import com.alvin.springshiro.model.Permission;
import com.alvin.springshiro.model.Role;
import com.alvin.springshiro.model.User;
import com.alvin.springshiro.service.UserRolePermissionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRolePermissionServiceImpl implements UserRolePermissionService {

    private final UserMapper userMapper;

    private final RoleMapper roleMapper;

    private final PermissionMapper permissionMapper;

    public UserRolePermissionServiceImpl(UserMapper userMapper, RoleMapper roleMapper, PermissionMapper permissionMapper) {
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
        this.permissionMapper = permissionMapper;
    }

    @Override
    public User getUserById(int userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public User getUserByUserName(String userName) {
        return userMapper.selectByUserName(userName);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int addRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public List<Role> getUserRols(int userId) {
        return roleMapper.selectRoleByUserId(userId);
    }

    @Override
    public List<Permission> getUserPermissions(int userId) {
        return permissionMapper.selectPermissonsByUserId(userId);
    }
}
